package com.ljfiction.book.http;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;

/**
 * Volley 请求成功的回掉
 */
public abstract class VolleyListenerInterface {
    //    public Context mContext;
    public static Response.Listener<String> mListener;
    public static Response.ErrorListener mErrorListener;

    public VolleyListenerInterface() {
//        this.mContext = context;

    }

    // 请求成功时的回调函数
    public abstract void onMySuccess(String result);

    // 请求失败时的回调函数
    public abstract void onMyError(VolleyError error, int errorNo);

    // 创建请求的事件监听
    public Response.Listener<String> responseListener() {
        mListener = new Response.Listener<String>() {

            @Override
            public void onResponse(String s) {



/*
                //获取http的userAgent
                String property = System.getProperty("http.agent");
                String path = "www.baidu.com";
                try {
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestProperty("http.agent", property + "JYC_App_1.0.0_adnroid");
                } catch (Exception e) {
                    e.printStackTrace();
                }
*/
                Gson gson = new Gson();
                final SimpleResult simpleResult = gson.fromJson(s, SimpleResult.class);
                if (simpleResult != null) {
                    /*if (simpleResult.isResult() == false) {
                        if (simpleResult.getCode() == 1002) {
                            //1002 异设备登录清空本地token
                            try {
                                PreferenceHelper.write(PreferenceHelper.DEFAULT_FILE_NAME, AppConfig.PREFER_TOKEN_TAG, "");
                                PreferenceHelper.write(PreferenceHelper.DEFAULT_FILE_NAME, AppConfig.PREFER_COOKIE_TAG, "");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            DialogView dialogView = new DialogView(AppManager.topActivity()) {
                                @Override
                                public void isdismiss(int tag) {
                                    if (tag == DialogView.CANCELTAG) {
                                        // 测试登陆后跳转到当前页
                                       *//* if (!AppManager.topActivity().getClass().getName().equals("MaMainActivity")) {

                                        }*//*
                                        AppManager.topActivity().startActivity(new Intent(AppManager.topActivity(), LoginOrRegisterActivity.class));
                                        AppManager.topActivity().overridePendingTransition(R.anim.slide_bottom_in, R.anim.slide_bottom_null);
                                    }
                                }
                            };
                            dialogView.showdialog2("", simpleResult.getErrormessage(), "知道了", "");
                        } else {
                            onMySuccess(s);
                            return;
                        }
                        onMySuccess(s);
                        return;
                    } else {
                        onMySuccess(s);
                        return;
                    }*/
                }

            }
        };
        return mListener;
    }

    // 创建请求失败的事件监听
    public Response.ErrorListener errorListener() {
        mErrorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                int errorNo;
                if (volleyError != null) {
                    if (volleyError.networkResponse != null) {
                        errorNo = volleyError.networkResponse.statusCode;
                    } else {
                        errorNo = -1;
                    }

                } else {
                    errorNo = -1;

                }
                onMyError(volleyError, errorNo);
            }
        };
        return mErrorListener;
    }
}
